package org.zhxy.zhxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.zhxy.zhxy.dao.AppointRecordDao;
import org.zhxy.zhxy.dao.UserDao;
import org.zhxy.zhxy.entity.AppointRecord;
import org.zhxy.zhxy.entity.Notice;
import org.zhxy.zhxy.entity.User;
import org.zhxy.zhxy.entity.dto.ClassRoomAppointAddDto;
import org.zhxy.zhxy.service.AppointRecordService;
import org.zhxy.zhxy.utils.UserHolder;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;

/**
 * 项目名称: zhxy
 * 作者: zhaojs
 * 创建日期: 2024年10月20日
 * 文件说明: 见类描述
 */
@Service
public class AppointRecordServiceImpl extends ServiceImpl<AppointRecordDao, AppointRecord> implements AppointRecordService {

    @Resource
    private AppointRecordDao appointRecordDao;

    @Resource
    private UserDao userDao;

    /**
     * 预约申请审核
     *
     * @param id
     * @param status
     * @return
     */
    @Override
    public boolean updateStatusById(Integer id, Integer status) {
        AppointRecord record = new AppointRecord();
        record.setId(id);
        record.setStatus(status);
        return appointRecordDao.updateById(record) > 0;
    }

    @Override
    public List<AppointRecord> selectRoomByTime(Long startTime, Long endTime) {
        List<AppointRecord> appointRecords = appointRecordDao.selectListByTime(startTime, endTime);
        return appointRecords;
    }

    @Override
    public void appoint(ClassRoomAppointAddDto classRoomAppointAddDto) {
        synchronized(this) {
            Integer id = UserHolder.getUser().getId();
            AppointRecord appointRecord = new AppointRecord();
            String roomId = classRoomAppointAddDto.getRoomId();
            //
            //           List<AppointRecord> appointRecords = appointRecordDao.selectListByParam(classRoomAppointAddDto);
            //           if (appointRecords.size()>0){
            //               throw new RuntimeException("该时间段已有预约,请重新选择");
            //           }
            appointRecord.setUserId(id);
            appointRecord.setRoomId(Integer.parseInt(classRoomAppointAddDto.getRoomId()));
            appointRecord.setStartTime(classRoomAppointAddDto.getStartTime());
            appointRecord.setEndTime(classRoomAppointAddDto.getEndTime());
            appointRecord.setReason(classRoomAppointAddDto.getReason());
            appointRecord.setApplyDate(classRoomAppointAddDto.getApplyDate());
            appointRecord.setStatus(0);
            appointRecord.setCreateTime(new Date());
            appointRecord.setUpdateTime(new Date());
            int insert = appointRecordDao.insert(appointRecord);
            if (insert == 0) {
                throw new RuntimeException("预约失败,请稍后再试");
            }
        }
    }

    @Override
    public List<AppointRecord> selectMyAppoint(String status) {
        Integer userId = UserHolder.getUser().getId();

        List<AppointRecord> appointRecords = appointRecordDao.selectListByUserIdAndStatus(userId, status);
        if ("1".equals(status)){
            //如果查询的是已经通过的申请 判断是否可以评论 如果当前时间大于预约时间则设置可以评论为true
            appointRecords.forEach(appointRecord -> {
                Long startTime = appointRecord.getStartTime();
                Long endTime = appointRecord.getEndTime();
                Long currentTime = System.currentTimeMillis();
                if (currentTime > endTime) {
                    appointRecord.setIsCanComment(true);
                }
            });
        }
        return appointRecords;
    }

    @Override
    public void canleAppoint(String id) {
        UpdateWrapper<AppointRecord> appointRecordUpdateWrapper = new UpdateWrapper<>();
        appointRecordUpdateWrapper.eq("id", id);
        appointRecordUpdateWrapper.set("status", 3);
        appointRecordDao.update(null, appointRecordUpdateWrapper);

    }

    @Override
    public List<AppointRecord> selectRoomAndIsAppointByTime(Long startTime, Long endTime) {
        List<AppointRecord> appointRecords = appointRecordDao.selectRoomAndIsAppointByTime(startTime, endTime);
        return appointRecords;
    }

    /**
     * 预约申请审核列表
     *
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public Page<AppointRecord> listAppoint(int page, int pageSize) {
        int offset = (page - 1) * pageSize;
        List<AppointRecord> appointRecords = appointRecordDao.selectAppointRecords(offset, pageSize);

        Page<AppointRecord> appointRecordPage = new Page<>();
        appointRecordPage.setTotal(appointRecordDao.countAppointRecords());
        appointRecordPage.setRecords(appointRecords);
        return appointRecordPage;
    }

    /**
     * 预约申请审核查询
     *
     * @param studentId
     * @param strApplyDate
     * @param startTime
     * @param endTime
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public Page<AppointRecord> queryAppoint(String studentId, String strApplyDate, Long startTime, Long endTime, int page, int pageSize) {
        LocalDate applyDate = null;
        if (StringUtils.isNotBlank(strApplyDate)) {
            applyDate = LocalDate.parse(strApplyDate);
        }

        int offset = (page - 1) * pageSize;
        List<AppointRecord> appointRecords = appointRecordDao.queryAppoints(studentId, applyDate, startTime, endTime, offset, pageSize);

        Page<AppointRecord> appointRecordPage = new Page<>();
        appointRecordPage.setTotal(appointRecordDao.countQueryAppoints(studentId, applyDate, startTime, endTime));
        appointRecordPage.setRecords(appointRecords);
        return appointRecordPage;
    }

    /**
     * 预约记录查询
     * @param studentId
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public Page<AppointRecord> queryAppointRecord(String studentId, int page, int pageSize) {
        int offset = (page - 1) * pageSize;
        List<AppointRecord> appointRecords = appointRecordDao.queryAppointRecords(studentId, offset, pageSize);

        Page<AppointRecord> appointRecordPage = new Page<>();
        appointRecordPage.setTotal(appointRecordDao.countQueryAppointRecords(studentId));
        appointRecordPage.setRecords(appointRecords);
        return appointRecordPage;
    }
}
